﻿
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
namespace SSLCertHelper
{

    //****************************************//
    // creater  zy
    // datetime 2023-09-04 14:36:02
    //****************************************//	
    class LogHelper
    {
        internal static readonly string RootPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", ""));
        private static LogHelper _instance;

        private FileStream logFile = null;

        private static readonly object objLock = new object();

        private LogHelper()
        {
            try
            {
                if (!Directory.Exists(RootPath + "\\log"))
                    Directory.CreateDirectory(RootPath + "\\log");
                logFile = File.OpenWrite(RootPath + "\\log\\sslupdate.log");
                long len = logFile.Length;
                logFile.Seek(len, SeekOrigin.Begin);

            }
            catch
            {

            }
        }

        public static LogHelper Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (objLock)
                    {
                        if (_instance == null)
                            _instance = new LogHelper();
                    }
                }

                return _instance;
            }
        }
        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="log"></param>
        /// <param name="args"></param>
        public void WriteLine(string log, params string[] args)
        {
            try
            {
                Console.WriteLine(log, args);
                byte[] data = Encoding.UTF8.GetBytes(DateTime.Now.ToString() + " : " + string.Format(log, args) + "\n");

                logFile.Write(data, 0, data.Length);
                logFile.Flush();
            }
            catch (Exception)
            {

            }
        }

        public static void WriteLog(string logTxt, params string[] args)
        {
            Instance.WriteLine(logTxt, args);
        }
        public static void WriteLog(string txt, Exception ex)
        {
            Instance.WriteLine(txt + ex.Message + "\n" + ex.StackTrace);
        }
    }


}
